package com.sca.api.system.domain;

import java.util.regex.Pattern;

/**
 * @Author: denglei
 * @Date: 2021/12/21 10:30
 * @Description:
 */
public class FlieNamefilter {
    //private static Pattern FilePattern = Pattern.compile("[\\\\/:*?\"<>|]");

    private static Pattern FilePattern = Pattern.compile("[\\s\\.:?<>|]"); //过滤规则

    public static String filenameFilter(String str) {
        return str == null ? null : FilePattern.matcher(str).replaceAll("");
    }

    public static void main(String[] args) {
        ///home/zcils/tools-service/bin/mapfiles/
        String str = "home/..  <>|logs/../:edata?";
        str="/home/zcils/tools-service/bin/mapfiles/dd.text";
        //String filenameFilter = filenameFilter(str);
        String filenameFilter = fileNameValidate(str);
        System.out.println(filenameFilter);
    }


    private static String fileNameValidate(String str) {

        String strInjectListStr = "../|./|/..| |<|>|:|?";

        str = str.toLowerCase();
        String[] badStrs = strInjectListStr.split("\\|");
        for (int i = 0; i < badStrs.length; i++) {
            if (str.indexOf(badStrs[i]) >= 0) {
                str = str.replace(badStrs[i], "");
            }
        }

        return str;
    }
}
